import axios from 'axios';
import { ElLoading } from 'element-plus';

axios.defaults.timeout = 10000;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
let loading;
const startLoading = () => {
  const options = {
    lock: true,
    text: "加载中...",
    background: 'rgba(0,0,1,0.5)'
  }
  loading = ElLoading.service(options);
}

const endLoading = () => {
  loading.close();
}

// 请求拦截
axios.interceptors.request.use((config) => {
  // 加载
  console.log("config",config);
  if(config.url=="/api/flush_process"){
    return config;
  }
  startLoading();
  return config;
})
// 响应拦截
axios.interceptors.response.use((response) => {
  // 结束loading
  endLoading();
  return response;
}, error => {
  // 结束loading
  endLoading();
  // 错误提醒
  return Promise.reject(error);
})

export default axios;
